import QtQuick
import QtQuick.Controls
import QtQuick.Shapes

import "../as_qml"

Window {
    id: imageRetreatmentWin
    width: 640
    height: 480
    visible: true
    title: qsTr("预处理图片")


    property color mTextColor: "#1a1c1e"
    property int leftSideListWidth: 150
    property int rightBottomSettingHeight: 150

    property var pictureList: ["当前图片列表为空"]
    property string pictureCurName: ""
    property int pictureCurIndex: 0

    // 设置当前显示图片
    function setPictureShow(index) {
        var size = myHandle.getPictureSize(openFileText.text + pictureList[index])
        if(size.width < 1 || size.height < 1) return
        pictureSizeText.text = size.width + "x" + size.height
        pictureCurIndex = index
        pictureCurName = pictureList[index]
        showImage.source = "file:///" + openFileText.text + pictureCurName
        curChoosePictureText.text = pictureCurName

    }

    function setTipsShow(flag, text) {
        tipsText.color = (flag === true) ? "green" : "red"
        tipsText.text = qsTr(text)
    }

    Connections {
        target: myHandle

        function onPictureOpened(filePath, fileList) {
            openFileText.text = filePath;
            pictureList = fileList;
        }

        function onCheckPictureOvered(faceNum) {
            showImage.source = "image://camera/check_face"
            faceNumText.text = qsTr(faceNum.toString())
        }

        function onRetreatmentDirectoryOpened(filePath) {
            saveFileText.text = filePath + "/"
        }
    }

    Rectangle {
        id: pictureListViewRect
        width: leftSideListWidth
        anchors.top: parent.top
        anchors.bottom: tipsBottomRect.top
        anchors.left: parent.left
//        color: "#f0f0f0"

        Shape {
            anchors.fill: parent

            ShapePath {
                strokeColor: "#707070"
                startX: pictureListViewRect.width
                startY: 0

                PathLine {
                    x: pictureListViewRect.width
                    y: pictureListViewRect.height
                }
            }
        }

        ListView {
            id: pictureListView
            anchors.fill: parent
            clip: true
            visible: true

            model: pictureList
            spacing: 1

            delegate: AsButton {
                width: pictureListView.width
                height: 22
                itemRadius: 0
                text: pictureList[index]

                onClicked: {
                    setPictureShow(index)
                }
            }

            ScrollBar.vertical: AsScrollBar {}
        }
    }

    Rectangle {
        id: showImageRect
        anchors.top: parent.top
        anchors.bottom: retreatmentSetting.top
        anchors.left: pictureListViewRect.right
        anchors.right: parent.right
        color: "#404040"

        Image {
            id: showImage
            width: parent.width
            height: parent.height
            cache: false
            fillMode: Image.PreserveAspectFit
        }
    }

    Column {
        id: retreatmentSetting
        height: rightBottomSettingHeight
        anchors.bottom: tipsBottomRect.top
        anchors.left: pictureListViewRect.right
        anchors.right: parent.right
        padding: 4
        spacing: 4

        Row {
            width: parent.width - parent.padding * 4
            height: 28
            spacing: 4

            AsText {
                id: openFileTitleText
                width: 50
                height: parent.height
                text: qsTr("图片路径:")
                verticalAlignment: Text.AlignVCenter
            }

            AsTextField {
                id: openFileText
                width: parent.width - openFileTitleText.width - openFileBtn.width
                height: parent.height
                placeholderText: qsTr("请选择需要打开的图片文件夹内的任一图片")
                color: mTextColor
                readOnly: true
                verticalAlignment: Text.AlignVCenter
            }

            AsButton {
                id: openFileBtn
                width: 80
                height: parent.height
                text: qsTr("选择路径")

                onClicked: {
                    var ret = myHandle.openPictureFile()
                    setTipsShow(ret, (ret === false) ? "操作取消" : "无")
                }
            }
        }

        Row {
            width: parent.width - parent.padding * 4
            height: 28
            spacing: 4

            AsText {
                id: saveFileTitleText
                width: 50
                height: parent.height
                text: qsTr("保存路径:")
                verticalAlignment: Text.AlignVCenter
            }

            AsTextField {
                id: saveFileText
                width: parent.width - saveFileTitleText.width - openFileBtn2.width
                height: parent.height
                placeholderText: qsTr("请选择保存处理后图片的路径")
                color: mTextColor
                readOnly: true
                verticalAlignment: Text.AlignVCenter
            }

            AsButton {
                id: openFileBtn2
                width: 80
                height: parent.height
                text: qsTr("选择路径")

                onClicked: {
                    var ret = myHandle.openRetreatmentFileDir()
                    setTipsShow(ret, (ret === false) ? "操作取消" : "无")
                }
            }
        }

        Row {
            width: parent.width - parent.padding * 8
            height: 28
            spacing: 4

            AsText {
                id: curChoosePictureTitle
                width: 50
                height: parent.height
                text: qsTr("当前选择:")
                verticalAlignment: Text.AlignVCenter
            }

            AsTextField {
                id: curChoosePictureText
                width: parent.width - curChoosePictureTitle.width - faceNumTitle.width - faceNumText.width - checkBtn.width - pictureSizeTitle.width - pictureSizeText.width
                height: parent.height
                placeholderText: qsTr("无")
                color: mTextColor
                readOnly: true
                verticalAlignment: Text.AlignVCenter
            }

            AsText {
                id: pictureSizeTitle
                width: 30
                height: parent.height
                text: qsTr("尺寸:")
                verticalAlignment: Text.AlignVCenter
            }

            AsTextField {
                id: pictureSizeText
                width: 70
                height: parent.height
                placeholderText: qsTr("0x0")
                color: mTextColor
                readOnly: true
                verticalAlignment: Text.AlignVCenter
            }

            AsText {
                id: faceNumTitle
                width: 50
                height: parent.height
                text: qsTr("人脸数量:")
                verticalAlignment: Text.AlignVCenter
            }

            AsTextField {
                id: faceNumText
                width: 22
                height: parent.height
                placeholderText: qsTr("0")
                color: mTextColor
                readOnly: true
                verticalAlignment: Text.AlignVCenter
            }

            AsButton {
                id: checkBtn
                width: 80
                height: parent.height
                text: qsTr("检验人脸")

                onClicked: {
                    var ret = myHandle.checkPicture_face(openFileText.text + pictureCurName)
                    setTipsShow(ret, (ret === false) ? "请先打开任一图片并选中" : "无")
                }
            }
        }

        Row {
            width: parent.width - parent.padding * 2
            height: 28
            spacing: 4

            AsButton {
                id: removeThisBtn
                width: 80
                height: parent.height
                text: qsTr("移除此项")

                onClicked: {
                    pictureList.splice(pictureCurIndex, 1)
                    var temp
                    pictureListView.model = temp
                    pictureListView.model = pictureList
                    setTipsShow(true, "无")
                }
            }

            AsButton {
                id: retreatmentThisBtn
                width: 80
                height: parent.height
                text: qsTr("处理此项")

                onClicked: {
                    var ret = myHandle.retreatmentPicture(openFileText.text + pictureCurName, saveFileText.text + pictureCurName)
                    setTipsShow(ret, (ret === false) ? "请先打开任一图片并确认保存路径存在" : "无")
                }
            }

            AsButton {
                id: retreatmentAllBtn
                width: 80
                height: parent.height
                text: qsTr("一键处理")

                onClicked: {
                    var ret = myHandle.retreatmentPictureAll(openFileText.text, saveFileText.text, pictureList)
                    setTipsShow(ret, (ret === false) ? "请先打开任一图片并确认保存路径存在" : "无")
                }
            }
        }
    }

    Rectangle {
        id: tipsBottomRect
        width: parent.width
        height: 28
        anchors.bottom: parent.bottom
        color: "#e0e0e0"

        Row {
            anchors.fill: parent

            AsText {
                id: tipsTitle
                width: 40
                height: parent.height
                text: qsTr("提示:")
                verticalAlignment: Text.AlignVCenter
                horizontalAlignment: Text.AlignHCenter
            }

            AsText {
                id: tipsText
                width: parent.width - tipsTitle.width
                height: parent.height
                text: qsTr("无")
                color: "green"
                verticalAlignment: Text.AlignVCenter
            }
        }
    }

}
